package cn.com.bo.util;

import java.math.BigDecimal;

import cn.com.bo.util.tools.ToolsForNumber;


public class AmountUtil
{
    /**
     * 
     * @Title: addAmount 
     * @Description: 金额相加
     * @param amount1
     * @param amount2
     * @return
     */
    public static String addAmount(String amount1,String amount2){
        String amount = ToolsForNumber.format(Float.parseFloat(amount1)+Float.parseFloat(amount2), 2);
        return amount.replace(",", "");   
    }
    
    public static String addDoubleAmount(String amount1,String amount2){
        BigDecimal b1 = new BigDecimal(amount1);
        BigDecimal b2 = new BigDecimal(amount2);
        return b1.add(b2).toString();   
    }
    
    /**
     * 
     * @Title: subAmount 
     * @Description:金额相减
     * @param amount1
     * @param amount2
     * @return
     */
    public static String subAmount(String amount1,String amount2){
        String amount = ToolsForNumber.format(Float.parseFloat(amount1)-Float.parseFloat(amount2), 2);
        return amount.replace(",", "");   
    }
    
    public static Float amountToFloat(String amount){
        return  Float.parseFloat(amount);
    }
    
    /** 
    * * 两个Double数相加 * 
    *  
    * @param v1 * 
    * @param v2 * 
    * @return Double 
    */  
    public static Double add(Double v1, Double v2) {  
       BigDecimal b1 = new BigDecimal(v1.toString());  
       BigDecimal b2 = new BigDecimal(v2.toString());  
       return new Double(b1.add(b2).doubleValue());  
    }  
      
    /**
     * 两个Double数相减
    * @Title: sub
    * @Description: TODO(两个Double数相减 )
    * @param @param v1
    * @param @param v2
    * @param @return    设定文件
    * @return Double    返回类型
    * @throws
     */
    public static Double sub(Double v1, Double v2) {  
       BigDecimal b1 = new BigDecimal(v1.toString());  
       BigDecimal b2 = new BigDecimal(v2.toString());  
       return new Double(b1.subtract(b2).doubleValue());  
    }  
      
    /** 
    * * 两个Double数相乘 * 
    *  
    * @param v1 * 
    * @param v2 * 
    * @return Double 
    */  
    public static Double mul(Double v1, Double v2) {  
       BigDecimal b1 = new BigDecimal(v1.toString());  
       BigDecimal b2 = new BigDecimal(v2.toString());  
       return new Double(b1.multiply(b2).doubleValue());  
    }  
      
      
    /** 
    * * 两个Double数相除，并保留scale位小数 * 
    *  
    * @param v1 * 
    * @param v2 * 
    * @param scale * 
    * @return Double 
    */  
    public static Double div(Double v1, Double v2, int scale) {  
       if (scale < 0) {  
        throw new IllegalArgumentException(  
          "The scale must be a positive integer or zero");  
       }  
       BigDecimal b1 = new BigDecimal(v1.toString());  
       BigDecimal b2 = new BigDecimal(v2.toString());  
       return new Double(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue());  
    }  
    
    
    public static boolean rangeInDefined(double current, double min, double max)  
    {  
        return Math.max(min, current) == Math.min(current, max);  
    }  
    
    /**
     * 
     * @param contrastValue 要对比的值
     * @param floatValue 上下浮动多少
     * @param baseValue 基础值
     * @return
     */
    public static boolean contrastValue(double contrastValue, double floatValue, double baseValue)  
    {  
    	double min = baseValue - floatValue;
    	double max = baseValue + floatValue;
    	return rangeInDefined(contrastValue,min,max);
    }  
    
    public static void main(String[] args)
    {
        Double a = 1764516.13;
        Double avg = div(a,12d,2);
        System.out.println("平均数："+avg);
        Double b = mul(avg,0.7);
        Double c = mul(avg,0.7);
        System.out.println("最小数："+b);
        System.out.println("最大数："+c);
        Double d = 0d;
        for(int i = 0;i<11 ; i++){
            Double f = add(b,mul(Math.random(),c)); 
            System.out.println(f);
            d = add(d,f);
        }
        System.out.println("随机想加:"+d);
        Double g = sub(a,d);
        System.out.println(g);
    }

}
